package top.xunfong.knowledge.service.impl;

import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.OSSAsyncClient;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.StaticCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
import com.aliyun.sdk.service.oss2.transport.BinaryData;
import top.xunfong.knowledge.service.OSSService;

import java.io.File;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

public class AliYunOSSService implements OSSService {

    // OSS配置信息 - 实际项目中建议放在配置文件中
    private static final String ENDPOINT = "https://oss-cn-chengdu.aliyuncs.com"; // 地域节点
    private static final String ACCESS_KEY_ID = "LTAI5tPAnopGjv5r9ShJzJHC";           // 访问密钥ID
    private static final String ACCESS_KEY_SECRET = "mIaxMNN90UmXNx1JRm5JKqRXmLOmbZ";   // 访问密钥Secret
    private static final String BUCKET_NAME = "xunfong-oss";               // 存储空间名称

    /**
     * 线程池
     */
    private ExecutorService executorService;

    public AliYunOSSService(ExecutorService executorService) {
        this.executorService = executorService;
    }

    @Override
    public String getServiceName() {
        return "aliyun";
    }

    @Override
    public String uploadFile(String localFilePath, String folder) {
        return uploadFile(new File(localFilePath), folder);
    }

    @Override
    public String uploadFile(File file, String folder) {
        if (!file.exists()) {
            throw new RuntimeException("文件不存在: " + file.getAbsolutePath());
        }
        String region = "cn-chengdu";
        CredentialsProvider provider = new StaticCredentialsProvider(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        CompletableFuture completableFuture = new CompletableFuture();
        completableFuture.completeAsync(
//        executorService.execute(
                () -> {
                    try (OSSAsyncClient client = OSSAsyncClient.newBuilder()
                            .region(region)
                            .credentialsProvider(provider)
                            .build()) {
                        PutObjectRequest request = PutObjectRequest.newBuilder()
                                .bucket(BUCKET_NAME)
                                .key(folder + file.getName())
                                .body(BinaryData.fromString(file.getAbsolutePath()))
                                .build();
                        CompletableFuture<PutObjectResult> putObjectResultCompletableFuture = client.putObjectAsync(request);
                        PutObjectResult join = putObjectResultCompletableFuture.join();
                        System.out.println(join);
                    } catch (Exception e) {
                        System.out.printf("main error:\n%s\n", e);
                    }
                    return null;
                });
        completableFuture.join();
        return null;
    }
}
